home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Kant Generator Pro 1.0.1 / source / kode new / kant build meat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  6.1 KB  |  242 lines  |  [TEXT/MMCC]

  1. #include "kant build meat.h"
  2. #include "kant build gui.h"
  3. #include "kant build files.h"
  4. #include "text twiddling.h"
  5. #include "util.h"
  6. #include "graphics.h"
  7. #include "window layer.h"
  8.  
  9. Str15            gRefLeading="\p»  &";
  10. Str15            gRefClosedLeading="\p«  &";
  11. Str15            gInstantLeading="\p          ";
  12.  
  13. static    Boolean            gIsRef;
  14. static    Boolean            gRefClosed;
  15.  
  16. void DealWithBuildContentClick(WindowPtr theWindow, Point thePoint)
  17. {
  18.     TEHandle        hTE;
  19.     short            selStart, selEnd, newSelStart;
  20.     Boolean            isSingleClick;
  21.     short            lineNum;
  22.     unsigned char    thisChar;
  23.     
  24.     hTE=GetWindowTE(theWindow);
  25.     isSingleClick=TRUE;
  26.     
  27.     if (AnyHighlightedQQ(theWindow))
  28.     {    /* treat this as a double-click if it's on currently highlighted line */
  29.         lineNum=CurrentLineNumber(hTE);
  30.         selStart=LineStart(hTE, lineNum);
  31.         selEnd=LineStart(hTE, lineNum+1)-1;
  32.         newSelStart=TEGetOffset(thePoint, hTE);
  33.         thisChar=(*(**hTE).hText)[newSelStart];
  34.         if ((newSelStart==selStart) && (RefHighlightedQQ(theWindow)))
  35.         {
  36.             DealWithArrowClick(theWindow, lineNum);
  37.             isSingleClick=FALSE;
  38.         }
  39.         else if ((newSelStart>=selStart) && (newSelStart<=selEnd))
  40.         {    /* is double-click */
  41.             DoEditRef(theWindow);
  42.             isSingleClick=FALSE;
  43.         }
  44.     }
  45.     
  46.     if (isSingleClick)
  47.     {    /* treat this as a single-click */
  48.         selStart=TEGetOffset(thePoint, hTE);
  49.         if (((*(**hTE).hText)[selStart])=='\r')
  50.             selStart--;
  51.         lineNum=LineNumberFromOffset(hTE, selStart);
  52.         HighlightLine(hTE, lineNum);
  53.         if ((RefHighlightedQQ(theWindow)) && (selStart==LineStart(hTE, lineNum)))
  54.             DealWithArrowClick(theWindow, lineNum);
  55.     }
  56. }
  57.  
  58. void DealWithArrowClick(WindowPtr theWindow, short lineNum)
  59. {
  60.     TEHandle        hTE;
  61.     ControlHandle    vScrollBar;
  62.     short            selStart, selEnd;
  63.     Str255            refName;
  64.     Boolean            alreadyOpen;
  65.     unsigned char    firstCharOfLine;
  66.     
  67.     hTE=GetWindowTE(theWindow);
  68.     vScrollBar=GetWindowVScrollBar(theWindow);
  69.     selStart=LineStart(hTE, lineNum);
  70.     firstCharOfLine=(*(**hTE).hText)[selStart];
  71.     alreadyOpen=(firstCharOfLine==gRefLeading[1]);
  72.     if (CurrentLineNumber(hTE)!=lineNum)
  73.         HighlightLine(hTE, lineNum);
  74.     
  75.     ZapDrawHook(hTE);
  76.     
  77.     if (alreadyOpen)
  78.     {
  79.         selStart=LineStart(hTE, lineNum);
  80.         selEnd=GetNextInstantiationOffset(theWindow);
  81.         TESetSelect(selStart, selStart+1, hTE);
  82.         TEDelete(hTE);
  83.         TEInsert(&gRefClosedLeading[1], 1, hTE);
  84.         selStart=LineStart(hTE, lineNum+1);
  85.         TESetSelect(selStart, selEnd, hTE);
  86.         TEDelete(hTE);
  87.         AdjustForEndScroll(vScrollBar, hTE);
  88.         HighlightLine(hTE, lineNum);
  89.         AdjustVScrollBar(vScrollBar, hTE);
  90.     }
  91.     else
  92.     {
  93.         GetHighlightedString(theWindow, refName);
  94.         selStart=LineStart(hTE, lineNum);
  95.         TESetSelect(selStart, selStart+1, hTE);
  96.         TEDelete(hTE);
  97.         TEInsert(&gRefLeading[1], 1, hTE);
  98.         selStart=LineStart(hTE, lineNum+1);
  99.         TESetSelect(selStart, selStart, hTE);
  100.         AddInstantiationsFromFile(GetWindowFS(theWindow), theWindow, refName);
  101.         AdjustForEndScroll(vScrollBar, hTE);
  102.         HighlightLine(hTE, lineNum);
  103.         AdjustVScrollBar(vScrollBar, hTE);
  104.     }
  105.     
  106.     RestoreDrawHook(hTE);
  107.     InvalRect(&(theWindow->portRect));
  108. }
  109.  
  110. void DoShowHideAll(WindowPtr theWindow, Boolean isShow)
  111. {
  112.     TEHandle        hTE;
  113.     short            lineNum;
  114.     short            numLines;
  115.     short            selStart;
  116.     unsigned char    firstCharOfLine;
  117.     unsigned char    charToMatch;
  118.     
  119.     hTE=GetWindowTE(theWindow);
  120.     numLines=TotalNumberOfLines(hTE);
  121.     charToMatch=isShow ? gRefClosedLeading[1] : gRefLeading[1];
  122.     
  123.     lineNum=0;
  124.     while (lineNum<TotalNumberOfLines(hTE))
  125.     /* note that TotalNumberOfLines may change if we hide or show anything */
  126.     {
  127.         selStart=LineStart(hTE, lineNum);
  128.         firstCharOfLine=(*(**hTE).hText)[selStart];
  129.         if (firstCharOfLine==charToMatch)
  130.         {
  131.             DealWithArrowClick(theWindow, lineNum);
  132.             UpdateTheWindow(theWindow);
  133.         }
  134.         lineNum++;
  135.     }
  136. }
  137.  
  138. void HighlightLine(TEHandle hTE, short lineNum)
  139. {
  140.     short            startOfLine;
  141.     unsigned char    firstCharOfLine;
  142.     
  143.     startOfLine=(**hTE).lineStarts[lineNum];
  144.     firstCharOfLine=(*((**hTE).hText))[startOfLine];
  145.     gIsRef=(firstCharOfLine!=gInstantLeading[1]);
  146.     TESetSelect(startOfLine+(gIsRef ? 3 : 5), (**hTE).lineStarts[lineNum+1], hTE);
  147.     gRefClosed=gIsRef && (firstCharOfLine==gRefClosedLeading[1]);
  148. }
  149.  
  150. Boolean RefHighlightedQQ(WindowPtr theWindow)
  151. {
  152.     if (theWindow==0L)
  153.         return TRUE;
  154.     
  155.     return (AnyHighlightedQQ(theWindow)) ? gIsRef : TRUE;
  156. }
  157.  
  158. Boolean RefClosedQQ(WindowPtr theWindow)
  159. {
  160.     if (theWindow==0L)
  161.         return FALSE;
  162.     
  163.     return (AnyHighlightedQQ(theWindow)) ? gRefClosed : FALSE;
  164. }
  165.  
  166. Boolean AddNameToTE(TEHandle hTE, Str255 theName, Boolean isRef)
  167. /* returns FALSE if no room left to insert */
  168. {
  169.     long            len, leadingLen;
  170.     unsigned char    returnChar='\r';
  171.     
  172.     len=theName[0];
  173.     leadingLen=(isRef ? gRefLeading[0] : gInstantLeading[0]);
  174.     if ((**hTE).teLength+len+leadingLen+1>32767)
  175.         return FALSE;
  176.     
  177.     if (isRef)
  178.         TEInsert(&gRefLeading[1], gRefLeading[0], hTE);
  179.     else
  180.         TEInsert(&gInstantLeading[1], gInstantLeading[0], hTE);
  181.     TEInsert(&theName[1], len, hTE);
  182.     TEInsert(&returnChar, 1, hTE);
  183.     
  184.     return TRUE;
  185. }
  186.  
  187. short GetNextInstantiationOffset(WindowPtr theWindow)
  188. {
  189.     TEHandle        hTE;
  190.     short            lineNum;
  191.     
  192.     hTE=GetWindowTE(theWindow);
  193.     lineNum=CurrentLineNumber(hTE);
  194.     do {}
  195.     while ((*(**hTE).hText)[(**hTE).lineStarts[++lineNum]]==gInstantLeading[1]);
  196.     
  197.     return (**hTE).lineStarts[lineNum];
  198. }
  199.  
  200. void GetHighlightedString(WindowPtr theWindow, Str255 theStr)
  201. {
  202.     GetStringFromLine(theWindow, theStr, CurrentLineNumber(GetWindowTE(theWindow)));
  203. }
  204.  
  205. void GetStringFromLine(WindowPtr theWindow, Str255 theStr, short lineNum)
  206. {
  207.     TEHandle        hTE;
  208.     short            selStart, selEnd;
  209.     Boolean            isRef;
  210.     
  211.     hTE=GetWindowTE(theWindow);
  212.     selStart=(**hTE).lineStarts[lineNum];
  213.     selEnd=(**hTE).lineStarts[lineNum+1]-1;
  214.     isRef=((*(**hTE).hText)[selStart]!=gInstantLeading[1]);
  215.     selStart+=isRef ? gRefLeading[0] : gInstantLeading[0];
  216.     Mymemcpy((Ptr)&theStr[1], (Ptr)((unsigned long)(*(**hTE).hText)+selStart), selEnd-selStart);
  217.     theStr[0]=selEnd-selStart;
  218. }
  219.  
  220. void GetRefNameFromInstantiation(WindowPtr theWindow, Str255 refName, short *stringIndex)
  221. {
  222.     short            index;
  223.     short            lineNum;
  224.     TEHandle        hTE;
  225.     short            offset;
  226.     unsigned char    firstChar;
  227.     
  228.     hTE=GetWindowTE(theWindow);
  229.     index=0;
  230.     lineNum=CurrentLineNumber(hTE);
  231.     do
  232.     {
  233.         index++;
  234.         offset=(**hTE).lineStarts[--lineNum];
  235.         firstChar=(*((**hTE).hText))[offset];
  236.     }
  237.     while (firstChar==gInstantLeading[1]);
  238.     
  239.     GetStringFromLine(theWindow, refName, lineNum);
  240.     *stringIndex=index;
  241. }
  242.